<?php
namespace Admin\Controller;

class AuthGroupController extends AdminController{
	
	public function index(){
		$auth = M('AuthGroup');
    	if(isset($_GET['name'])){
            $map['title']    =   array('like', '%'.(string)I('get.name').'%');
        }
    	$list = $auth->where($map)->order("id Asc")->select();
    	$this->assign('list', $list);
    	
		$this->meta_title = '权限管理';
		$this->display();
	}
	
	public function add(){
		if(IS_AJAX){
			$auth = D('AuthGroup');
            $data = $auth->create();
            if($data){
                if($insert_id = $auth->add()){
                    $msg_data['status']	= 1;
	    			$msg_data['msg']	= '新增成功';
	    			$msg_data['jump_url'] = U('index');
	    			$this->ajaxReturn($msg_data);
                } else {
                    $msg_data['status']	= 0;
	    			$msg_data['msg']	= '新增失败';
	    			$this->ajaxReturn($msg_data);
                }
            } else {
                $msg_data['status']	= 0;
	    		$msg_data['msg']	= $auth->getError();
	    		$this->ajaxReturn($msg_data);
            }
		}else{
			$this->meta_title = '新增用户组';
			$this->display();
		}
	}
	
	public function edit($id = 0){
		if(IS_AJAX){
			$auth = D('AuthGroup');
            $data = $auth->create();
            if($data){
                if($insert_id = $auth->save()){
                    $msg_data['status']	= 1;
	    			$msg_data['msg']	= '更新成功';
	    			$msg_data['jump_url'] = U('index');
	    			$this->ajaxReturn($msg_data);
                } else {
                    $msg_data['status']	= 0;
	    			$msg_data['msg']	= '更新失败';
	    			$this->ajaxReturn($msg_data);
                }
            } else {
                $msg_data['status']	= 0;
	    		$msg_data['msg']	= $auth->getError();
	    		$this->ajaxReturn($msg_data);
            }
		}else{
			$this->info	= M('AuthGroup')->find($id);
			$this->meta_title = '新增用户组';
			$this->display('add');
		}
	}
	
	public function access(){
		if(IS_POST){
			$rules = I('post.rules');
			$group_id = I('post.id');
			$str_rule_id = '';
			if($rules){
				foreach ($rules as $key){
					$data = array();
					$menu_info = M('Menu')->field('title,url')->find($key);
					//查找节点是否存在 如果不存在则添加到节点
					$auth_rule = M('AuthRule');
					if($rule_info = $auth_rule->where("name='$menu_info[url]'")->field('id')->find()){
						$str_rule_id .=  $rule_info['id'].',';
					}else{
						$data['name']		= $menu_info['url'];
						$data['title']		= $menu_info['title'];
						$data['type']		= 1;
						$data['status']		= 1;
						$data['condition']	= '';
						//添加到节点
						$insert_ruleid = $auth_rule->add($data);
						$str_rule_id .=  $insert_ruleid.',';
					}
				}
				
				$str_rule_id = substr($str_rule_id, 0,-1);
				$auth_group_data['id']		= $group_id;
				$auth_group_data['rules']	= $str_rule_id;
				if(M('AuthGroup')->save($auth_group_data)!==false){
					$this->success('操作成功');
				}else{
					$this->success('操作失败');
				}
			}else{
				$auth_group_data['id']		= $group_id;
				$auth_group_data['rules']	= '';
				if(M('AuthGroup')->save($auth_group_data)!==false){
					$this->success('操作成功');
				}else{
					$this->success('操作失败');
				}
			}
		}else{
			$id	= I('get.id');
			$this->this_group = M('AuthGroup')->find($id);
			$this->access_list	= D('menu')->getMenuList($where='');
			//读取当前权限节点
			$rule_menu_check = array();
			$rules = M('AuthGroup')->where("id=$id")->find();
			if($rules['rules']){
				$rule_list = M('AuthRule')->where("id in($rules[rules])")->select();
				
				foreach ($rule_list as $val){
					$menu_info = M('Menu')->where("url='$val[name]'")->field('id')->find();
					$rule_menu_check[]	= $menu_info['id'];
				}
			}
			$this->rule_menu_check	= $rule_menu_check;
			$this->meta_title = '访问授权';
			$this->display();
		}
	}
	
	//更新状态
    public function changeStatus(){
    	$id = I('get.id');
    	$method = I('get.method');
    	$map['id'] =   $id;
    	switch (strtolower($method)){
    		case 'forbiduser'://禁用
    			$data    =  array('status' => 0);
    			if(M('AuthGroup')->where($map)->save($data)!==false){
    				$this->success('操作成功');
    			}else{
    				$this->error('操作失败');
    			}
                break;
            case 'resumeuser'://启用
    			$data    =  array('status' => 1);
    			if(M('AuthGroup')->where($map)->save($data)!==false){
    				$this->success('操作成功');
    			}else{
    				$this->error('操作失败');
    			}
                break;
            case 'deleteuser'://删除
            	if(M('AuthGroup')->where($map)->delete()!==false){
            		$this->success('操作成功');
            	}else{
            		$this->error('操作失败');
            	}
                break;
            default:
                $this->error('参数非法');
    	}
    }
}
?>